package leetcode.median;

public class $134_CanCompleteCircuit {
    public int canCompleteCircuit(int[] gas, int[] cost) {
        int n = gas.length;
        int totalRemainGas = 0;
        for (int i = 0; i < n;i++) {
            totalRemainGas += gas[i] - cost[i];
        }
        if (totalRemainGas < 0) {
            return -1;
        }

        int currentGas = 0;
        int index = 0;
        int i = 0;
        while (i < n) {
            currentGas += gas[i] - cost[i];
            if (currentGas < 0) {
                currentGas = 0;
                index = i + 1;
                i++;
            } else {
                i++;
            }
        }
        return index;
    }
}
